﻿using System;
using System.Configuration;
using System.Text.RegularExpressions;

namespace SocialFront
{
    public static class Library
    {
        /// <summary>
        /// Takes the name of an appsetting in the web.config and returns the umbraco nice url to the node
        /// Assumes that the App Setting exists and contains a valid Umbraco node id
        /// </summary>
        /// <param name="appSettingName"></param>
        /// <returns></returns>
        public static string NiceUrlFromAppSettings(string appSettingName)
        {
            if (ConfigurationManager.AppSettings[appSettingName] == null)
            {
                throw new Exception(String.Format("The app setting {0} does not exist in the web.config. It should exist and contain the id of an Umbraco node.", appSettingName));
            }

            int nodeId = 0;
            if (!int.TryParse(ConfigurationManager.AppSettings[appSettingName], out nodeId))
            {
                throw new Exception(String.Format("The app setting {0} in the web.config does not contain a valid integer. It should contain the id of an Umbraco node.", appSettingName));
            }
            return umbraco.library.NiceUrl(nodeId);
        }

        /// <summary>
        /// Nices the URL.
        /// </summary>
        /// <param name="nodeId">The node id.</param>
        /// <returns></returns>
        /*public static string NiceUrl(int nodeId)
        {
            return umbraco.library.NiceUrl(nodeId);
        }*/

        private static string StripHTML(string htmlString)
        {
            //This pattern Matches everything found inside html tags;
            //(.|\n) - > Look for any character or a new line
            // *?  -> 0 or more occurences, and make a non-greedy search meaning
            //That the match will stop at the first available '>' it sees, and not at the last one
            //(if it stopped at the last one we could have overlooked
            //nested HTML tags inside a bigger HTML tag..)
            // Thanks to Oisin and Hugh Brown for helping on this one...
            string pattern = @"<(.|\n)*?>";
            return Regex.Replace(htmlString, pattern, string.Empty);
        }
    }
}
